package com.cskaoyan.controller.system.aspect;

import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.po.MarketLog;
import com.cskaoyan.mapper.MarketLogMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.SourceLocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author stone
 * @date 2022/11/12 11:21
 */
@Component
@Aspect
public class HelloAspect {

    @Pointcut("@annotation(com.cskaoyan.controller.system.annotation.RecordOperation)")
    public void pointcut1() {
    }


    @Autowired
    MarketLogMapper mapper;

    //todo:稍微有一些缺陷
    @After("pointcut1()")
    public void method2(JoinPoint joinPoint) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userIP = getIP(request);
        Signature signature = joinPoint.getSignature();
        String methodName = signature.getName();
        MarketLog marketLog = new MarketLog();

        Subject subject = SecurityUtils.getSubject();
        // 获得Principal信息 → realm的doGetAuthorizationInfo方法构造的认证信息里的第一个参数
        MarketAdmin primaryPrincipal = null;
        try {
            primaryPrincipal = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();
            marketLog.setAdmin(primaryPrincipal.getUsername());
            marketLog.setResult("");
        } catch (Exception e) {
            marketLog.setAdmin("匿名用户");
            marketLog.setResult("用户帐号或密码不正确");
        }

        Session session = subject.getSession();
        Integer loginState = (Integer) session.getAttribute("loginState");
        if (loginState == 0) {
            marketLog.setStatus(true);
        }
        if (loginState == 605) {
            marketLog.setStatus(false);
        }

        marketLog.setIp(userIP);
        marketLog.setType(1);
        marketLog.setAction(methodName);
        marketLog.setAddTime(new Date());
        marketLog.setUpdateTime(new Date());
        marketLog.setComment("");


        mapper.insertLog(marketLog);
    }

    /**
     * 获取访问ip
     *
     * @param request
     * @return
     * @throws Exception
     */
    public static String getIP(final HttpServletRequest request) throws Exception {
        if (request == null) {
            return "unknown";
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    }
}
